package com.shiro;

import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.mgt.DefaultSessionStorageEvaluator;
import org.apache.shiro.mgt.DefaultSubjectDAO;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;

@Configuration
public class ShiroConfig {

    @Autowired
    private UsernameRealm loginRealm;

    @Autowired
    private ApplicationContext applicationContext ;

    @Bean
    public SecurityManager securityManager(){
        DefaultSecurityManager defaultSecurityManager = new DefaultWebSecurityManager();
        DefaultSubjectDAO defaultSubjectDAO = new DefaultSubjectDAO();
        DefaultSessionStorageEvaluator defaultSessionStorageEvaluator = new DefaultSessionStorageEvaluator();
        defaultSubjectDAO.setSessionStorageEvaluator(defaultSessionStorageEvaluator);
        defaultSecurityManager.setSubjectDAO(defaultSubjectDAO);
        defaultSecurityManager.setRealm(loginRealm);
        return  defaultSecurityManager;
    }

    @Bean(name = "shiro_config")
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager){

        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        shiroFilterFactoryBean.setLoginUrl("index.html");
        LinkedHashMap filterMap = new LinkedHashMap();
        //filterMap.put("anon", new AnonymousFilter());
        filterMap.put("jwt",new SessionFilter(applicationContext));
        shiroFilterFactoryBean.setFilters(filterMap);
        LinkedHashMap<String,String> fiterRealmMap = new LinkedHashMap<String,String>();
        fiterRealmMap.put("/css/**" , "anon") ;
        fiterRealmMap.put("/admin/*.html","anon");
        fiterRealmMap.put("/admin/images/**" , "anon") ;
        fiterRealmMap.put("/image/**" , "anon") ;
        fiterRealmMap.put("/admin/adminLogin","anon");
        fiterRealmMap.put("/admin/**/*.html","anon");
        fiterRealmMap.put("/admin/**/*.js","anon");
        fiterRealmMap.put("/admin/**/*.css","anon");
        fiterRealmMap.put("/admin/img/*","anon");
        fiterRealmMap.put("/admin/viewstyle/**","anon");
        fiterRealmMap.put("/admin/viewstyle/*","anon");
        fiterRealmMap.put("/login/*","anon");
        fiterRealmMap.put("/js/**","anon");
        fiterRealmMap.put("/admin/css/*","anon");
        fiterRealmMap.put("/admin/js/*","anon");
        fiterRealmMap.put("/","anon");
        fiterRealmMap.put("/upload/**","anon");
        fiterRealmMap.put("/upload/**/**","anon");
        fiterRealmMap.put("/uploadImg/**","anon");

        fiterRealmMap.put("/tmp/**","anon");
        fiterRealmMap.put("/swagger-ui.html","anon");

        fiterRealmMap.put("/**","jwt,authc");

        fiterRealmMap.put("/userInfo/exportExcelByUser","anon");

        //shiroFilterFactoryBean.setSuccessUrl("/admin/main.html");
        //shiroFilterFactoryBean.setLoginUrl("/index.html");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(fiterRealmMap);
        return shiroFilterFactoryBean;
    }
}
